package edu.nf.llmtest.service.Listenter;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.nf.llmtest.common.config.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ Author bin
 * @ Create  2025/10/27 下午3:32
 * mq 生产者
 **/
@Service
public class MqProducer {
    private static final Logger log = LoggerFactory.getLogger(MqProducer.class);
    private final RabbitTemplate rabbitTemplate;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public MqProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void send(String type, Object data) {
        try {
            // 封装消息
            Map<String, Object> message = new HashMap<>();
            message.put("type", type);
            message.put("data", data);

            // 转 JSON
            String json = objectMapper.writeValueAsString(message);

            // 发送消息（如果你用的是默认队列，exchange 可以留空）
            rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME,
                    RabbitConfig.ROUTING_KEY, json);
            log.info("✅ 发送成功 -> type:{} " , type);
            log.info("✅ 详情 -> {} " , json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
